package architect.datastructure;

import java.util.LinkedList;

/**
 * 栈的链表实现
 *
 * @author guanxin
 * @date 2018/5/7 10:44
 */

public class LinkedListStack<E> {
    // 链表
    LinkedList<E> list;
    // 栈顶元素下标
    int top;
    // 栈大小
    int maxSize;

    public LinkedListStack() { }

    public LinkedListStack(int maxSize) {
        this.maxSize = maxSize;
        top = -1;
        list = new LinkedList<>();
    }

    // 入栈
    public void push(E e) {
        list.add(e);
        top++;
    }

    // 出栈
    public E pop() {
        top--;
        return list.removeLast();
    }

    // 获取而不弹出
    public E peek() {
        return list.getLast();
    }

    // 是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    // 是否已满
    public boolean isFull() {
        return top == (this.maxSize - 1);
    }
}
